logo头像
Snippet 博客主题

Kotlin之Elvis 操作符

Elvis 操作符的优先级

首先要注意到,Elvis 操作符的优先级是比较低的,特别是比加减乘除的运算符低,混合运算时一定要记得加上括号。比如:

1
2
3
4
5
6
fun <T> Iterable<T>.contentHashCode(): Int {
return fold(1) {
hash, element ->
hash * 31 + (element?.hashCode() ?: 0)
}
}

这里如果不用括号将 element?.hashCode() ?: 0 括起来,编译器就会认为这句表达式是 (hash * 31 + element?.hashCode()) ?: 0,出现编译错误。

与 ?. 配合使用时的问题

Elvis 操作符与安全调用符 ?. 配合使用时,一定要考虑到安全调用符前后是否为空,否则就会带来流程控制混乱的问题。对于任何一个下列的表达式:

1
val v = a?.b ?: c

因为 ?. 的优先级比 ?: 高,首先计算 a?.b,按照安全调用符的规则,如果 a == null 则结果为 null,执行 c,但如果 a.b == null,也会执行 c。也就是说,它的执行逻辑是这样的:

1
2
var temp = if(a != null) a.b else null
val v = if(temp != null) temp else c

它等价于:

1
val v = if(a == null || a.b == null) c else a.b

实际使用时一定要注意 ?. 前后是否都可能为 null。

?: 与流程控制语句的搭配使用

我发了一个 Kotlin 写的前序遍历二叉树的 Gist,地址在这里:PreOrderTraversing.kt,整个项目:DataStructureLearning-Kotlin
非递归遍历二叉树的代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
private fun preOrderLoop(
root: Node? = this.root,
list: MutableList<Int> = mutableListOf()
): List<Int> {
val stack = ArrayDeque<Node>()
stack.push(root ?: return list)
while (stack.isNotEmpty()) {
val nodeNow = stack.pop()!!
list += nodeNow.value
nodeNow.right?.let { stack.push(it) }
nodeNow.left?.let { stack.push(it) }
}
return list
}

第二句很有意思。ArrayDeque 不能容纳 null,一旦插入 null 就会抛出 NPE,而我们的函数要求 root 为 null 时返回一个空的 List,所以这里 push() 的参数写成 root ?: return list,这句代码的逻辑如下:

1
2
if(root == null) return list
stack.push(root)

此外,Elvis 还可以配合 break 和 continue 来控制循环流程。

改良 Elvis

Elvis 操作符很方便,但只能连接表达式,我们可以写一个扩展函数来作为加强版的 Elvis 操作符。

1
2
3
4
5
6
inline infix fun <T : Any> T?.ifNull(block: (T?) -> T): T {
if (this == null) {
return block(this)
}
return this
}

使用方式:

1
2
3
4
val file = java.io.File("C:\\FakeFile")
val parent = file.parent ifNull {
// do something here
}
支付宝打赏 微信打赏

如果文章对你有帮助,欢迎点击上方按钮打赏作者

上一篇